/**
 * @作者:黎谢之
 * @创建时间：2021/7/20
 * @修改时间：2021/7/20
 */
package com.example.senior_bug.Service;

import com.example.senior_bug.EntityClass.*;
import com.example.senior_bug.Repository.*;
import com.example.senior_bug.ToolClass.Result;
import com.example.senior_bug.enm.MsgId;
import com.example.senior_bug.utils.ResultUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class AdministratorService {
    @Autowired
    private ChannelRepository channelRepository;
    @Autowired
    private CategoryRepository categoryRepository;
    @Autowired
    private BrandRepository brandRepository;
    @Autowired
    private GoodRepository goodRepository;
    @Autowired
    private SKURepository skuRepository;
    @Autowired
    private BusinessRepository businessRepository;
    @Autowired
    private SKUMessageRepository skuMessageRepository;


    /*验证管理员信息
     * 7.20创建
     * 7.23修改
     * */
    public Result Verification(String account, String password) {
        //判断注册是否成功(管理员使用提前设置的账号密码)
        if (password.equals("e10adc3949ba59abbe56e057f20f883e") && account.equals("13152680940")) {
            return ResultUtils.success(true);
        } else {
            return ResultUtils.success(false);
        }
    }

    /*管理员管理商品页面获取所有商品
     * 7.20创建
     * 7.23修改
     * */
    public Result GetAllSKU() {
        //当商品数量大于0时才返回商品列表
        if (skuRepository.findAll().size() >= 0) {
            List<SKU> skus = new ArrayList<>();

            for (SKU item :
                    skuRepository.findAll()) {
                Brand brand = brandRepository.findById(goodRepository.findById(item.getGoodId()).getBrandId());
                //为临时变量赋值(不存入数据库的数据)
                item.setCategoryName(channelRepository.findById
                        (categoryRepository.findById(brand.getCategoryId())
                                .getChannelId()).getName() + "/" + categoryRepository.findById(brand.getCategoryId()).getName());
                item.setBrandName(brand.getName());
                skus.add(item);
            }

            return ResultUtils.success(skus);
        } else
            return ResultUtils.error(MsgId.ANYSKU_NOT_FOUND);
    }

    /*管理员审核页面获取所有待审核商品
     * 7.20创建
     * 7.23修改
     * */
    public Result GetAllReviewedSku() {
        List<SKU> ReviewedSku = new ArrayList<>();
        for (SKU item :
                skuRepository.findAll()) {
            if (item.getState().equals("待审核")) {
                Brand brand = brandRepository.findById(goodRepository.findById(item.getGoodId()).getBrandId());
                //为临时变量赋值
                item.setCategoryName(channelRepository.findById
                        (categoryRepository.findById(brand.getCategoryId())
                                .getChannelId()).getName() + "/" + categoryRepository.findById(brand.getCategoryId()).getName());
                item.setBrandName(brand.getName());
                ReviewedSku.add(item);
            }
        }
        //当数量大于0时返回
        if (ReviewedSku.size() >= 0) {
            return ResultUtils.success(ReviewedSku);
        } else {
            return ResultUtils.error(MsgId.ANYSKU_NOT_FOUND);
        }
    }

    /*管理员对商品审核后向商家发送信息
     * 1.发送的信息对象(skuMessage)
     * 7.20创建
     * 7.23修改
     * */
    public Result SendMessage(SKUMessage skuMessage) {
        //查找是否存在该商品
        if (skuRepository.existsById(skuMessage.getSku().getId())) {
            //获取商品对象
            SKU sku = skuRepository.findById(skuMessage.getSku().getId());
            //绑定关系
            skuMessage.setSku(sku);
            //保存信息
            SKUMessage skuMessage1 = skuMessageRepository.saveAndFlush(skuMessage);

            //获取商家并将信息保存至商家消息列表
            Business business = businessRepository.findById(sku.getBusinessId());
            business.getSkuMessages().add(skuMessage1);
            businessRepository.saveAndFlush(business);

            return ResultUtils.success(true);
        } else {
            return ResultUtils.error(MsgId.SKU_NOT_FOUND);
        }
    }

    /*管理员修改商品状态
     *
     * */
    public Result ModifySkuState(String newState, int skuId) {
        if (skuRepository.existsById(skuId)) {
            if (newState.equals("待审核") || newState.equals("已下架") || newState.equals("已上架") || newState.equals("未通过")) {
                SKU sku = skuRepository.findById(skuId);
                sku.setState(newState);
                SKU sku1 = skuRepository.saveAndFlush(sku);
                if (sku1.getState().equals(newState)) {
                    return ResultUtils.success(true);
                } else {
                    return ResultUtils.error(MsgId.MODIFY_ERROR);
                }
            } else {
                return ResultUtils.error(MsgId.ILLEGAL_STATE);
            }
        } else {
            return ResultUtils.error(MsgId.SKU_NOT_FOUND);
        }
    }

    /*管理员添加新Channel
     * 缺陷功能
     * */
    public Result AddNewChannel(String channelName) {
        Channel channel = new Channel();
        channel.setName(channelName);
        channelRepository.saveAndFlush(channel);
        return ResultUtils.success(true);
    }

    /*管理员添加新的二级分类
     * 缺陷功能
     * */
    public Result AddNewCategory(String categoryName, String channelName) {
        Category category = new Category();
        category.setName(categoryName);
        Channel channel = channelRepository.findByName(channelName);
        category.setChannelId(channel.getId());
        Category category1 = categoryRepository.saveAndFlush(category);
        channel.getCategories().add(category1);
        channelRepository.saveAndFlush(channel);
        return ResultUtils.success(true);
    }

    /*管理员添加品牌
     * 缺陷功能
     * */
    public Result AddNewBrand(String brandName, String categoryName) {
        Brand brand = new Brand();
        brand.setName(brandName);

        Category category = categoryRepository.findByName(categoryName);

        brand.setCategoryId(category.getId());

        Brand brand1 = brandRepository.saveAndFlush(brand);

        category.getBrands().add(brand1);
        categoryRepository.saveAndFlush(category);
        return ResultUtils.success(true);
    }

    /*管理员添加Good
     * 缺陷功能
     * */
    public Result AddNewGood(String goodName, String brandName) {
        Good good = new Good();
        good.setName(goodName);

        Brand brand = brandRepository.findByName(brandName);

        good.setBrandId(brand.getId());

        Good good1 = goodRepository.saveAndFlush(good);

        brand.getGoods().add(good1);
        brandRepository.saveAndFlush(brand);
        return ResultUtils.success(true);
    }

}
